package com.colin.app.activity.view.textview;

import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.TransformationMethod;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.help.LogHelp;
import com.colin.library.view.AutoTextView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 文本TextView
 * Handler的简单实用与防止内存泄漏
 * 枚举
 */
public class TextViewActivity extends AppActivity implements ViewContract.ViewView {
    private ViewContract.ViewPresenter mViewPresenter = null;
    //文字太长，文字省略为止
    private TextUtils.TruncateAt[] truncateAts = new TextUtils.TruncateAt[]{
            TextUtils.TruncateAt.START,//开始位置省略
            TextUtils.TruncateAt.MIDDLE,//中间位置省略
            TextUtils.TruncateAt.END,//结束为止省略
    };

    private int[] units = new int[]{
            TypedValue.COMPLEX_UNIT_PX,
            TypedValue.COMPLEX_UNIT_DIP,
            TypedValue.COMPLEX_UNIT_SP
    };


    private TextView tv_ellipsize = null;
    private TextView text_style_more = null;
    private AutoTextView text_auto_list = null;

    private int index = 0;

    private MyHandler handlerEllipsize = new MyHandler(this) {
        @Override
        public void handleMessage(Message msg) {
            index = ++index % truncateAts.length;
            tv_ellipsize.setEllipsize(truncateAts[index]);
            if (null != handlerEllipsize)
                handlerEllipsize.sendMessageDelayed(handlerEllipsize.obtainMessage(), 2000);
        }
    };
    //大小写转换
    private MyHandler handlerTransformationMethod = new MyHandler(this) {
        @Override
        public void handleMessage(Message msg) {
            int what = 0;
            if (msg.what == 0) {
                //大写
                tv_SetTransformationMethod.setTransformationMethod(new MyTransformationMethod(true));
                what = 1;
            } else {
                //小写
                tv_SetTransformationMethod.setTransformationMethod(new MyTransformationMethod());
                what = 0;
            }
            if (null != handlerEllipsize)
                handlerTransformationMethod.sendEmptyMessageDelayed(what, 2000);
        }
    };

    private TextView tv_SetTransformationMethod = null;

    private MyHandler handlerSetTextSize = new MyHandler(this) {
        @Override
        public void handleMessage(Message msg) {
            index = ++index % units.length;
            tv_setTextSize.setTextSize(units[index], 24.0f);
            if (null != handlerEllipsize)
                handlerSetTextSize.sendMessageDelayed(handlerSetTextSize.obtainMessage(), 2000);
        }
    };
    private TextView tv_setTextSize = null;
    private boolean textMore = false;

    @Override
    protected void onDestroy() {
        if (null != handlerEllipsize) {
            handlerEllipsize = null;
        }
        if (null != handlerTransformationMethod) {
            handlerTransformationMethod = null;
        }
        if (null != handlerSetTextSize) {
            handlerSetTextSize = null;
        }

        if (null != mViewPresenter) {
            mViewPresenter.destroy();
            mViewPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }


    @Override
    public int initViewLayoutID() {
        return R.layout.activity_text_view;
    }

    @Override
    public void initView() {
        tv_ellipsize = (TextView) findViewById(R.id.tv_ellipsize);
        tv_SetTransformationMethod = (TextView) findViewById(R.id.tv_SetTransformationMethod);
        tv_setTextSize = (TextView) findViewById(R.id.tv_setTextSize);
        text_style_more = (TextView) findViewById(R.id.text_style_more);
        text_auto_list = (AutoTextView) findViewById(R.id.text_auto_list);
        text_auto_list.setTextList(getTextList());
    }



    @Override
    public void initData() {
        mViewPresenter.setData();
        handlerEllipsize.sendMessage(handlerEllipsize.obtainMessage());
        handlerTransformationMethod.sendEmptyMessage(0);
        handlerSetTextSize.sendEmptyMessage(0);
    }

    @Override
    public void initListener() {
        this.text_style_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogHelp.e("getLineCount-->>" + String.valueOf(text_style_more.getLineCount()) + "\tgetHeight-->>" + String.valueOf(text_style_more.getHeight()));
                if (textMore) {
                    textMore = false;
                    text_style_more.setEllipsize(null); // 展开
                    text_style_more.setSingleLine(textMore);
                } else {
                    textMore = true;
                    text_style_more.setEllipsize(TextUtils.TruncateAt.END); // 收缩
                    text_style_more.setSingleLine(textMore);
                }
            }
        });
    }

    class MyTransformationMethod implements TransformationMethod {
        private boolean upperCase = false;

        public MyTransformationMethod(boolean... upperCase) {
            this.upperCase = (upperCase == null || upperCase.length <= 0) ? false : upperCase[0];
        }

        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            if (this.upperCase) {
                return TextUtils.isEmpty(source) ? "" : source.toString().toUpperCase(getResources().getConfiguration().locale) + "大写";
            } else {
                return TextUtils.isEmpty(source) ? "" : source.toString().toLowerCase(getResources().getConfiguration().locale) + "小写";
            }
        }

        @Override
        public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {
        }
    }

    /**
     * 内部类      handler正宗写法
     */
    class MyHandler extends Handler {
        // 弱引用 ，防止内存泄露
        private WeakReference<TextViewActivity> textViewActivityWeakReference = null;

        public MyHandler(TextViewActivity textViewActivity) {
            this.textViewActivityWeakReference = new WeakReference<TextViewActivity>(textViewActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 通过  软引用  看能否得到activity示例
            TextViewActivity textViewActivity = textViewActivityWeakReference.get();
            // 防止内存泄露
            if (null != textViewActivity) {// 如果当前Activity，进行UI的更新

            } else {// 没有实例不进行操作

            }
        }
    }
    private List<AutoTextView.AutoEntry> getTextList() {
        List<AutoTextView.AutoEntry> entryList = new ArrayList<>();
        AutoTextView.AutoEntry autoEntry =  null;
        for (int i = 0; i < 5; i++) {
            autoEntry = new AutoTextView.AutoEntry("张三获得收益","5000"+String.valueOf(i)+"元","www.baidu.com");
            entryList.add(autoEntry);
        }
        return  entryList ;
    }

}
